package com.javasm.service.Impl;

import com.alibaba.fastjson.JSON;
import com.javasm.dao.AdminMenuDao;
import com.javasm.dao.AdminUserDao;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.service.LoginService;
import com.javasm.utils.MybatisHealper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class LoginServiceImpl implements LoginService {
    @Override
    public AdminUser getuserByusername(String username) {
        SqlSession getsqlsession = MybatisHealper.getsqlsession();
        AdminUserDao mapper = getsqlsession.getMapper(AdminUserDao.class);
        AdminUser adminUser = mapper.getuserByusername(username);

        MybatisHealper.backAndSaveSqlSession(getsqlsession);
        return adminUser;


    }

    @Override
    public void updateUserLoginTime(Long uid) {
        SqlSession getsqlsession = MybatisHealper.getsqlsession();
        AdminUserDao mapper = getsqlsession.getMapper(AdminUserDao.class);
        mapper.updateUserLoginTime(uid);

        MybatisHealper.backAndSaveSqlSession(getsqlsession);
    }


    @Override
    public List<AdminMenu> ListMenuWithSubMenuByUid(Long uid) {
        SqlSession sqlSession = MybatisHealper.getsqlsession();
        AdminMenuDao mapper = sqlSession.getMapper(AdminMenuDao.class);
        List<AdminMenu> adminMenus = mapper.ListMenuWithSubMenuByUid(uid);
        System.out.println(JSON.toJSONString(adminMenus));
        MybatisHealper.backAndSaveSqlSession(sqlSession);
        return adminMenus;
    }
}
